package com.secsbrain.cmdb.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.secsbrain.cmdb.biz.IRackService;
import com.secsbrain.sso.core.SsoHelper;
import com.secsbrain.cmdb.dal.model.Cabinet;
import com.secsbrain.cmdb.dal.model.Rack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.cmdb.biz.ICabinetService;
import com.secsbrain.cmdb.biz.impl.CommonService;
import com.secsbrain.common.ResponseResult;

/**
 * 机柜控制层
 * @author zhaoyi
 *
 */
@RestController
@RequestMapping("api/cabinet")
public class CabinetController{
	
    @Autowired
    ICabinetService cabinetService;
    
    @Autowired
    IRackService rackService;
    
    @Autowired
    CommonService commonService;
    
    /**
     * 功能描述: <br>
     * 查询列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseResult<?> queryList(Cabinet params) {
    	Page<Cabinet> page=new Page<Cabinet>(0, 10);
    	Page<Cabinet> result=cabinetService.selectPage(page, params);
    	Map<Long, String> idcMap=commonService.getIdcMap();
    	for(Cabinet cabinet:result.getRecords()){
    		cabinet.setIdcName(idcMap.get(cabinet.getIdcId()));
    		Rack rackParam=new Rack();
    		rackParam.setCabinetId(cabinet.getId());
			int rackCount=rackService.selectCount(rackParam);
    		cabinet.setRackCount(rackCount+"");
    	}
    	return new ResponseResult(result);
    }
    
    /**
     * 功能描述: <br>
     * 通过机房查询机柜列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/queryByIdc", method = RequestMethod.GET)
    public ResponseResult<?> queryByIdc(Cabinet params) {
    	List<Cabinet> list=new ArrayList<>();
    	if(params.getIdcId()==null){
    		return new ResponseResult(list);
    	}
    	
    	list=cabinetService.selectList(params);
    	return new ResponseResult(list);
    }
    
    /**
     * 功能描述: <br>
     * 新增
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult<?> insert(@RequestBody Cabinet entity) {
    	entity.setCreateTime(new Date());
    	entity.setCreator(SsoHelper.currentUser().getNickname());
    	cabinetService.insertSelective(entity);
    	return new ResponseResult();
    }
    
    /**
     * 功能描述: <br>
     * 更新
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseResult<?> update(@RequestBody Cabinet entity) {
    	entity.setUpdateTime(new Date());
    	entity.setUpdator(SsoHelper.currentUser().getNickname());
    	cabinetService.updateSelectiveById(entity);
    	return new ResponseResult();
    }
    
    /**
     * 功能描述: <br>
     * 删除
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult<?> delete(@RequestBody Cabinet entity) {
    	cabinetService.deleteById(entity.getId());
    	return new ResponseResult();
    }
    
}
